import { _decorator, Component, Node, Animation } from 'cc'
import { initParamsNumber, initParamsTrigger, StateMachine } from '../../Base/StateMachine'
import { FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum'
import { IdleSubStateManagerIron } from '../IronSkeleton/IdleSubStateManagerIron'
import { DeathSubStateMachineIron } from '../IronSkeleton/DeathSubStateMachineIron'
import State from '../../Base/State'
const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = BurstStateMachine
 * DateTime = Fri Jul 22 2022 14:31:30 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = BurstStateMachine.ts
 * FileBasenameNoExtension = BurstStateMachine
 * URL = db://assets/Scripts/Burst/BurstStateMachine.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
const BASE_URL = 'texture/burst'
@ccclass('BurstStateMachine')
export class BurstStateMachine extends StateMachine {
  // 初始化参数
  initParams() {
    this.params.set(PARAMS_NAME_ENUM.IDLE, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.ATTACK, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.DEATH, initParamsTrigger())
    this.params.set(PARAMS_NAME_ENUM.DIRECTION, initParamsNumber())
  }

  // 初始化状态机
  initStateMachine() {
    this.stateMachines.set(PARAMS_NAME_ENUM.IDLE, new State(this, `${BASE_URL}/idle`))
    this.stateMachines.set(PARAMS_NAME_ENUM.ATTACK, new State(this, `${BASE_URL}/attack`))
    this.stateMachines.set(PARAMS_NAME_ENUM.DEATH, new State(this, `${BASE_URL}/death`))
  }

  async init() {
    // 添加动画组件
    this.animationComponent = this.addComponent(Animation)
    this.initParams()
    this.initStateMachine()
    // 确保资源资源加载
    await Promise.all(this.waitingList)
  }
  run() {
    switch (this.currentState) {
      case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
      case this.stateMachines.get(PARAMS_NAME_ENUM.DEATH):
      case this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK):
        if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK)
        } else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.DEATH)
        } else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
        } else {
          // 为了触发子状态机的改变
          this.currentState = this.currentState
        }
        break
      default:
        this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
    }
  }
}
